var t = !1,
    a = !1;
Page({
    data: {
        page: 1,
        last_page: 0,
        keyword: '',
        list: {},
        goodsList: [],
        cartList: [],
        purchase_id: 0,
        options: [],
    },
    onLoad: function(options) {
        var a = this;
        a.setData({
            options: options
        });
        a.goodList(options);
    },
    goodList: function() {

        var t = this;
        a = !1, t.setData({
            page: 1,
            list: []
        });
        var i = t.data.page || 1;

        getApp().request({
            url: getApp().api.default.purchase_goods_list,
            data: {
                purchase_id: t.data.options.purchase_id,
                page: i
            },
            success: function(e) {
                if (e.data.purchaseGoods) {
                    t.setData({
                        purchase_id: e.data.purchaseGoods.id
                    });
                }
                if (!e.data.userInfo) {
                    getApp().core.showModal({
                        title: "提示",
                        content: '团长不存在',
                        showCancel: !1,
                        success: function(e) {
                            getApp().core.redirectTo({
                                url: "/pages/index/index"
                            })
                        }
                    })
                }
                0 == e.code && (0 == e.data.list.length && (a = !0), t.setData({
                    page: i + 1
                }), t.setData({
                    userInfo: e.data.userInfo,
                    purchaseInfo: e.data.purchaseInfo,
                    list: e.data.list,
                    last_page: e.data.page_count,
                }));
              if (e.data.purchaseInfo) { 
                wx.setNavigationBarTitle({
                  title: e.data.purchaseInfo.name
                })
              }
            },
            complete: function() {}
        });
    },
    ConfirmFunc(e) {
        console.info(e)
    },
    addGoods: function() {
        getApp().core.redirectTo({
            url: "/pages/index/add"
        })
    },
    cartdec: function(e) {
        var a = this;
        var goods_id = e.currentTarget.dataset.gid;
        var list = a.data.list;
        for (var i = 0; i < list.length; i++) {
            if (goods_id == list[i].id) {
                if ((list[i].gnum - 1) >= 0) {
                    list[i].gnum = list[i].gnum - 1;
                    this.setData({
                        list: list
                    });
                    a.cartUpdate(list[i].gnum, goods_id);
                    break;
                } else {
                    wx.showToast({
                        title: '数量不能为0'
                    });
                }

            }
        }
    },
    cartadd: function(e) {
        var a = this;
        var goods_id = e.currentTarget.dataset.gid;
        var list = a.data.list;
        for (var i = 0; i < list.length; i++) {
            if (goods_id == list[i].id) {
                if ((list[i].gnum + 1) <= list[i].goods_num) {
                    list[i].gnum = parseInt(list[i].gnum) + parseInt(1);
                    this.setData({
                        list: list
                    });
                    a.cartUpdate(list[i].gnum, goods_id);
                    break;
                } else {
                    wx.showToast({
                        title: '库存不足了！'
                    });
                }

            }
        }
    },
    buy: function(e) {
        var a = this;
        var list = a.data.cartList;
        var buyList = [];
        var merList = [];
        for (var i = 0; i < list.length; i++) {

            var index = merList.indexOf(list[i].mch_id);

            if (index == -1) {
                merList.push(list[i].mch_id)
            }

        }
        var s = [];
        for (var x = 0; x < merList.length; x++) {
            var arr2 = [];
            for (var y = 0; y < list.length; y++) {
                var arr1 = {};
                if (merList[x] == list[y].mch_id) {
                    arr1.cart_id = list[y].id;
                    arr2.push(arr1);
                }
            }
            s.push({
                mch_id: merList[x],
                goods_list: arr2

            });
        }
        if (s) {
            var t = [];
            getApp().core.showLoading({
                    title: "正在提交",
                    mask: !0
                }),
                getApp().core.navigateTo({
                    url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(s) + "&type=1"
                }), getApp().core.hideLoading();
        }
    },
    cartUpdate: function(num, goods_id) {
        var a = this;
        getApp().request({
            url: getApp().api.default.purchase_goods_cart,
            method: "post",
            data: {
                num: num,
                goods_id: goods_id,
                purchase_id: a.data.purchase_id
            },
            success: function(t) {
                if (t.code == 0) {
                    a.setData({
                        cartList: t.cartList
                    });
                }
                return t.code;
            }
        });

    },
    onShareAppMessage: function(ops) {
        var a = this;
        return {
            title: '欢迎加入我的购物团',
            path: 'pages/index/list?purchase_id=' + a.data.options.purchase_id,
            success: function(res) {
                console.log("转发成功:" + JSON.stringify(res));
            },
            fail: function(res) {
                console.log("转发失败:" + JSON.stringify(res));
            }
        }
    },
    // 下拉刷新
    onPullDownRefresh: function() {
        console.info("下拉刷新");
        this.setData({
            page: 1,
        });
        this.goodList();
    },
    // 到达底部加载更多
    onReachBottom: function() {
        console.info("到达底部加载更多");
        if (this.data.last_page >= this.data.page) {
            this.goodList();
        }
    },




})